﻿using Lonewolf.Entity;
using Lonewolf.Infrastructure;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Lonewolf.Service
{
    public partial class RoleInfoService
    {
        public async Task<JsonResult<SaveEntity>> SaveAsync(RoleInfo model)
        {
            var result = new JsonResult<SaveEntity>();
            try
            {
                if (model == null)
                    throw new ArgumentNullException(nameof(model));
                RoleInfo roleInfo;
                if (model.Id.IsNullOrEmpty())//新增方法
                {
                    roleInfo = await Query().FirstOrDefaultAsync(p => p.RoleName == model.RoleName);
                    if (roleInfo != null)
                    {
                        result.message = "系统中已有此角色，角色名称重复，请重新输入！";
                        return result;
                    }
                    else
                    {
                        roleInfo = new RoleInfo
                        {
                            RoleName = model.RoleName
                        };
                        await this.InsertAsync(roleInfo);
                    }
                }
                else//修改方法
                {
                    roleInfo = await Query().FirstOrDefaultAsync(p => p.Id == model.Id);
                    if (roleInfo == null)
                    {
                        result.message = "找不到要修改的数据";
                        return result;
                    }
                    else
                    {
                        roleInfo.LastChangeTime = DateTime.Now;
                        roleInfo.RoleName = model.RoleName;
                        Update(roleInfo);
                    }
                }
                var flag = await SaveChangesAsync();
                result.success = flag;
                result.message = flag ? "保存成功" : "保存失败";
                result.Entity = new SaveEntity
                {
                    Id = roleInfo.Id
                };
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.code = 500;
            }
            return result;

        }
        public async Task<JsonResult<string>> DeleteAsync(string[] Id)
        {
            var result = new JsonResult<string>();
            try
            {
                if (Id == null)
                    throw new ArgumentNullException(nameof(Id));
                foreach (string item in Id)
                {
                    Delete(p => p.Id == item);
                }
                var flag = await SaveChangesAsync();
                result.success = flag;
                result.message = flag ? "删除成功！" : "删除失败！";
            }
            catch (Exception ex)
            {
                result.code = 500;
                result.message = ex.Message;
            }
            return result;
        }
        public async Task<JsonResult<string>> DletePseudoAsync(string[] Id)
        {
            var result = new JsonResult<string>();
            try
            {
                if (Id == null)
                    throw new ArgumentNullException(nameof(Id));
                foreach (string item in Id)
                {
                    DeleteForge(p => p.Id == item);
                }
                var flag = await SaveChangesAsync();
                result.success = flag;
                result.message = flag ? "删除成功！" : "删除失败!";
            }
            catch (Exception ex)
            {
                result.code = 500;
                result.message = ex.Message;
            }
            return result;

        }
    }
}
